package com.ohimy.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.ohimy.dao.impl.GroupUserShipDao;
import com.ohimy.dao.impl.MessageDao;
import com.ohimy.dao.impl.ScrapDao;
import com.ohimy.dao.impl.UserDao;
import com.ohimy.dao.impl.UserFriendShipDao;
import com.ohimy.entity.Group;
import com.ohimy.entity.GroupUserShip;
import com.ohimy.entity.Message;
import com.ohimy.entity.Scrap;
import com.ohimy.entity.User;
import com.ohimy.entity.UserFriendShip;

@Service("profileService")
public class ProfileService {

	@Resource(name = "messageDao")
	private MessageDao messageDao;

	@Resource(name = "userDao")
	private UserDao userDao;

	@Resource(name = "groupUserShipDao")
	private GroupUserShipDao groupUserShipDao;

	@Resource(name = "userFriendShipDao")
	private UserFriendShipDao userFriendShipDao;

	@Resource(name = "scrapDao")
	private ScrapDao scrapDao;

	/**
	 * 得到未读信息数量
	 * 
	 * @return
	 */
	public int getUnreadMsg(Message message) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Message.class);
		criteria.add(Restrictions.eq("toId", message.getToId())).add(
				Restrictions.eq("status", 0));
		return messageDao.findByCriteria(criteria).size();
	}

	/**
	 * 得到用户留言总数
	 * 
	 * @return
	 */
	public int getScrapTotal(User user) {
		Criterion[] criterions = new Criterion[] { Restrictions.eq("owner",
				user) };
		return scrapDao.findByCriteria(criterions).size();
	}

	/**
	 * 得到该用户的好友，只显示9个
	 * 
	 * @return
	 */
	public List<User> getUsersFriends(User user) {
		List<User> result = new ArrayList<User>();
		Criterion[] criterions = new Criterion[] { Restrictions
				.eq("user", user) };
		for (UserFriendShip uf : userFriendShipDao.findByCriteria(0, 9, null,
				criterions)) {
			result.add(uf.getFriend());
		}
		return result;
	}

	/**
	 * 得到会员加入的圈子,只显示4个
	 * 
	 * @param user
	 * @return
	 */
	public List<Group> getUsersGroups(User user) {
		List<Group> result = new ArrayList<Group>();
		Criterion[] criterions = new Criterion[] { Restrictions
				.eq("user", user) };
		for (GroupUserShip gu : groupUserShipDao.findByCriteria(0, 4, null,
				criterions)) {
			result.add(gu.getGroup());
		}
		return result;
	}

	/**
	 * 留言板内容的ajax分页
	 * 
	 * @param id
	 * @param firstResult
	 * @param maxResult
	 * @return
	 */
	public List<Scrap> getScrapByPager(long id, int firstResult, int maxResult) {
		Criterion[] criterions = new Criterion[] { Restrictions.eq("owner",
				scrapDao.get(id)) };
		return scrapDao.findByCriteria(firstResult, maxResult, Order
				.desc("created"), criterions);
	}

	/**
	 * 保存并返回留言
	 * 
	 * @param scrap
	 */
	public List<Scrap> saveScrap(Scrap scrap) {
		List<Scrap> result = new ArrayList<Scrap>();
		long reId = scrapDao.save(scrap);
		result.add(scrapDao.get(reId));
		return result;
	}

	/**
	 * 通过ID返回用户对象
	 * 
	 * @param id
	 * @return
	 */
	public User getUserById(long uid) {
		return userDao.get(uid);
	}

	// ////////////////////////////
	public void setMessageDao(MessageDao messageDao) {
		this.messageDao = messageDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public void setGroupUserShipDao(GroupUserShipDao groupUserShipDao) {
		this.groupUserShipDao = groupUserShipDao;
	}

	public void setUserFriendShipDao(UserFriendShipDao userFriendShipDao) {
		this.userFriendShipDao = userFriendShipDao;
	}

	public void setScrapDao(ScrapDao scrapDao) {
		this.scrapDao = scrapDao;
	}

}
